package com.gopay.common.manager.ptbank.impl;


import com.gopay.common.constants.cust.UserInfoConstants;
import com.gopay.common.cps.manager.gsrt.GsrtCustTenderMasterQueryManager;
import com.gopay.common.domain.UserInfo;
import com.gopay.common.domain.cust.CustCorpInfo;
import com.gopay.common.domain.cust.CustPersonInfo;
import com.gopay.common.domain.gsrt.GsrtCustTenderMaster;
import com.gopay.common.domain.ptbank.invo.HfCommonHead;
import com.gopay.common.domain.ptbank.invo.ObjectAddRequest;
import com.gopay.common.manager.ptbank.PtObjectManager;
import com.gopay.common.user.dao.CustCorpInfoQueryDAO;
import com.gopay.common.user.dao.CustPersonInfoQueryDAO;
import com.gopay.common.user.dao.UserInfoQueryDAO;
import com.gopay.common.util.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 标的信息 manager
 * PTObjectQueryManagerImpl.java
 * 2016年4月25日
 * @author jlj
 *  www.gopay.com.cn Inc.All rights reserved.
 */
@Service("ptObjectManager")
public class PtObjectManagerImpl implements PtObjectManager {

    @Resource(name = "custPersonInfoQueryDao")
    private CustPersonInfoQueryDAO custPersonInfoQueryDAO;

    @Resource(name = "custCorpInfoQueryDao")
    private CustCorpInfoQueryDAO custCorpInfoQueryDAO;

    @Resource(name = "userInfoQueryDao")
    private UserInfoQueryDAO userInfoQueryDAO;

    @Resource(name = "gsrtCustTenderMasterQueryManager")
    private GsrtCustTenderMasterQueryManager gsrtCustTenderMasterQueryManager;

    /**
     * 获取借款人信息
     * @param custId
     * @return
     */
    @Override
    public Map<String, Object> getCustInfo(String custId) {
        Map<String, Object> map = null;
        UserInfo userInfo = userInfoQueryDAO.getByCustId(custId).size() > 0 ? userInfoQueryDAO.getByCustId(custId).get(0) : null;
        if(userInfo != null) {
            map = new HashMap<>();
            if(UserInfoConstants.USER_TYPE_PERSON.equals(userInfo.getUserType())) {
                CustPersonInfo custPersonInfo = custPersonInfoQueryDAO.getByCustId(custId);
                    map.put("custName", custPersonInfo.getCustName());
                    map.put("certType", custPersonInfo.getCertType());
                    map.put("certNo", custPersonInfo.getCertNo());
            } else if(UserInfoConstants.USER_TYPE_CORP_ADMIN.equals(userInfo.getUserType())) {
                CustCorpInfo custCorpInfo = custCorpInfoQueryDAO.getByCustId(custId);
                map.put("custName", custCorpInfo.getCorpName());
                map.put("certType", custCorpInfo.getLegalPersonCertType());
                map.put("certNo", custCorpInfo.getLegalPersonCertNo());
            }
        }
        return map;
    }

    /**
     * 组装标的新增请求报文
     * @param gsrtCustTenderMaster
     * @return
     */
    @Override
    public ObjectAddRequest getObjectAddRequest(GsrtCustTenderMaster gsrtCustTenderMaster, Map<String, Object> params) {

        ObjectAddRequest objectAddRequest = new ObjectAddRequest();

        // 必填项
        objectAddRequest.setPROID(gsrtCustTenderMaster.getGopayOrderId()); // 支付机构标的ID 暂时写成国付宝订单号
        objectAddRequest.setMERCUSTID(gsrtCustTenderMaster.getMerCustId()); // 支付机构平台ID p2p平台custId
        objectAddRequest.setBIDNAME(gsrtCustTenderMaster.getBidName()); // 标的名称
        objectAddRequest.setBORRTOTAMT(gsrtCustTenderMaster.getMerTxnAmt().toString()); // 发标金额
        objectAddRequest.setYEARRATE(gsrtCustTenderMaster.getInterest().toString()); // 发标年化利率
        objectAddRequest.setBORRNAME(params.get("custName").toString());
        objectAddRequest.setBORRCERTTYPE(getCertType(params.get("certType").toString()));
        objectAddRequest.setBORRCERTID(params.get("certNo").toString());

        // 选填项
        objectAddRequest.setLOANPERIOD(gsrtCustTenderMaster.getLoanLife().toString()); // LOANPERIOD 借款期限
        return objectAddRequest;

    }

    /**
     * 组织报文头信息
     * @return
     */
    @Override
    public HfCommonHead getHfCommonHead(String method) {
        HfCommonHead hfCommonHead = new HfCommonHead();

        String orderDate = DateUtils.getCurrDate(DateUtils.FILEDATE_PATTERM);
        String orderId = DateUtils.getSystemDate().toString();
        hfCommonHead.setVersion("10");
        hfCommonHead.setMethod(method);
        hfCommonHead.setOrderdate(orderDate);
        hfCommonHead.setOrderid(orderId);
        hfCommonHead.setOrgcode("00001010");
        hfCommonHead.setOperatorid("");
        hfCommonHead.setLoginpassword("");
        return hfCommonHead;
    }

    /**
     * 转换证件类型
     * @param certType
     * @return
     */
    private String getCertType(String certType) {
        switch (certType) {
            case "01" :
                certType = "00";
                break;
            case "02" :
                certType = "05";
                break;
            case "03" :
                certType = "02";
                break;
            case "07" :
                certType = "01";
                break;
            case "08" :
                certType = "06";
                break;
            default :
                certType = "07";
                break;
        }
        return certType;
    }
}
